﻿import gs.TweenLite;
import mx.utils.Delegate;
/**
* ...
* @author Default
*/

class game.HintDoor {
	
	private var _mcDoor:MovieClip; 
	private var _nInitialX:Number;
	private var _nMinX:Number = 0;
	private var _nMaxX:Number = 0;
	private var _nDx:Number = 0;
	private var _nPixelsLeft:Number = 135;
	private var _bShouldIncreasePixels:Boolean = true;
	
	public function HintDoor(doorMC:MovieClip, ms:Number) {
		_mcDoor = doorMC;
		
		resetPosition();
		trace (ms)
		setInterval (this, "increasePixelsLeft", ms)
		
		addActions();
	}
	
	private function increasePixelsLeft():Void {
		if (_bShouldIncreasePixels) {
			_nPixelsLeft++;
			_nPixelsLeft = Math.min(_nPixelsLeft, 135)
			_mcDoor.hintmeter._y = 19 + (1 - (_nPixelsLeft / 135)) * 100;
		}
	
	}
	
	public function stop() {
		_bShouldIncreasePixels = false;
	}
	
	public function start() {
		_bShouldIncreasePixels = true;
	}
	
	public function resetPosition():Void {
		var tween:TweenLite = new TweenLite (_mcDoor, 27 / (_mcDoor._x - 549), { _x:549 } );
	}
	
	private function addActions ():Void {
		_mcDoor.onPress = Delegate.create(this, pressDoor);
		_mcDoor.onMouseUp = Delegate.create(this, releaseDoor);
	}
	
	private function limitPosition():Void {
		_mcDoor._x = Math.min (Math.max (_mcDoor._parent._xmouse - _nDx, _nMinX), _nMaxX);
		_nPixelsLeft -= _mcDoor._x - _nMinX;
		_nMinX = _mcDoor._x;
		
		this._mcDoor._parent.hintdoorshadow._x = this._mcDoor._x - 6;
		
		_mcDoor.hintmeter._y = 19 + (1 - (_nPixelsLeft / 135)) * 100;
		
		if (!this._mcDoor.hitTest(_mcDoor._parent._xmouse, _mcDoor._parent._ymouse)) releaseDoor ();
	}
	//limites: 549, 684
	private function pressDoor():Void {
		_mcDoor.onEnterFrame = Delegate.create(this, limitPosition);
		_nDx = _mcDoor._parent._xmouse - _mcDoor._x;
		_nMinX = _mcDoor._x;
		_nMaxX = Math.min (_mcDoor._x + _nPixelsLeft, 684);
	}
	
	/*
	135 - > 0
	0   -> 100
	*/
	
	private function releaseDoor():Void {
		_mcDoor.onEnterFrame = null;
	}
	
}